package cn.px.activiti.listener;

import cn.hutool.core.collection.CollectionUtil;
import cn.px.common.util.BeanUtils;
import cn.px.common.util.SecurityUtils;
import cn.px.dao.dto.upms.SysUserDTO;
import cn.px.dao.dto.upms.SysUserDeptDTO;
import cn.px.dao.enums.SysRoleCodeEnum;
import cn.px.dao.mapper.upms.SysUserDeptMapper;
import cn.px.dao.mapper.upms.SysUserMapper;
import cn.px.dao.vo.SysUserDeptVO;
import cn.px.dao.vo.SysUserVO;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description ${DESCRIPTION}
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2019,安徽阡陌网络科技有限公司 All rights reserved.
 */
public class CurrentDeptHandlerCandidateUsers implements TaskListener {

    @Override
    public void notify(DelegateTask delegateTask) {
        System.out.println("进入CurrentDeptHandlerCandidateUsers=========");
        String userId = SecurityUtils.getUser().getId();
        SysUserDeptMapper userDeptMapper = (SysUserDeptMapper) BeanUtils.getBean("sysUserDeptMapper");
        SysUserMapper userMapper = (SysUserMapper) BeanUtils.getBean("sysUserMapper");
        List<SysUserDeptVO> deptVOS = userDeptMapper.selectVOByUserId(new SysUserDeptDTO().setUserId(userId));
        if (CollectionUtil.isNotEmpty(deptVOS)) {
            List<SysUserVO> users = userMapper.selectDeptRoleUser(new SysUserDTO().setDeptIds(deptVOS.stream().map(item -> item.getDeptId()).collect(Collectors.toList())).setRoleCode(SysRoleCodeEnum.PURCHASE_MANAGER_PROJECT.name()));
            delegateTask.setAssignee(users.get(0).getUserId());
            delegateTask.addCandidateUsers(users.stream().map(item -> item.getUserId()).collect(Collectors.toList()));
        }
    }
}
